package com.ggk911.pharmacy_system.interceptor;

import com.ggk911.pharmacy_system.util.RestResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 控制器通知注解
 *
 * @author ggk-911
 * @version 1.0 2023/4/3 14:22
 */
@ControllerAdvice
public class ControllerExceptionHandler {
    @ResponseBody
    @ExceptionHandler(ArithmeticException.class)
    public RestResult handlerException1() {
        return RestResult.error("算术运算异常ArithmeticException");
    }

    @ResponseBody
    @ExceptionHandler(NumberFormatException.class)
    public RestResult handlerException2() {
        return RestResult.error("数字格式异常NumberFormatException");
    }

    @ResponseBody
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public RestResult handlerException3() {
        return RestResult.error("删除失败，该信息可能正在被使用");
//        return RestResult.error("违反了完整性约束（外键、主键或唯一键）SQLIntegrityConstraintViolationException");
    }

    @ResponseBody
    @ExceptionHandler(IOException.class)
    public RestResult handlerException4() {
        return RestResult.error("IOException");
    }


}
